﻿
Partial Class WebForm_ManageClearVersion
    Inherits System.Web.UI.Page
#Region "事件"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack() Then
            Me.BindDDLDivision()
            Me.BindDDLSignature()
            Me.BindDDLCategory()
            Me.BindDDLUploadType()
            Me.BindGV()
        End If
    End Sub

    Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
        '檢查此版本的Signature是否已經被Locked
        Dim vManager As New VersionManager(Me.VersionControl1.MonthCode, Me.ddlSignature.SelectedValue, Me.HeaderTB)
        If vManager.Locked = True Then
            MyObj.ShowMessage("Version Locked.")
            Return
        End If

        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        Dim Success As Boolean = True
        Dim ErrorMessage As String = ""
        Dim TableName As String = Me.HeaderTB & Me.ddlCategory.SelectedValue & Me.VersionControl1.MonthCode

        sql.Length = 0
        sql.AppendFormat("Update {0} ", TableName)
        sql.AppendFormat("set Mon1=null,Mon2=null,Mon3=null,Mon4=null,Mon5=null,Mon6=null ")
        sql.AppendFormat(",Mon7=null,Mon8=null,Mon9=null,Mon10=null,Mon11=null,Mon12=null ")
        sql.AppendFormat(",Mon13=null,Mon14=null,Mon15=null,Mon16=null,Mon17=null,Mon18=null ")
        sql.AppendFormat(",Mon19=null,Mon20=null,Mon21=null,Mon22=null,Mon23=null,Mon24=null ")
        Select Case Me.ddlCategory.SelectedValue
            Case "SFReport"
                sql.AppendFormat(",Mon25=null,Mon26=null,Mon27=null,Mon28=null,Mon29=null,Mon30=null ")
                sql.AppendFormat(",Mon31=null,Mon32=null,Mon33=null,Mon34=null,Mon35=null,Mon36=null ")
                'sql.AppendFormat(",Memo1=null,Memo2=null,Memo3=null,Memo4=null,Memo5=null,Memo6=null ")
                'sql.AppendFormat(",Memo7=null,Memo8=null,Memo9=null,Memo10=null,Memo11=null,Memo12=null ")
                'sql.AppendFormat(",Memo13=null,Memo14=null,Memo15=null,Memo16=null,Memo17=null,Memo18=null ")
                'sql.AppendFormat(",Memo19=null,Memo20=null,Memo21=null,Memo22=null,Memo23=null,Memo24=null ")
                'sql.AppendFormat(",Memo25=null,Memo26=null,Memo27=null,Memo28=null,Memo29=null,Memo30=null ")
                'sql.AppendFormat(",Memo31=null,Memo32=null,Memo33=null,Memo34=null,Memo35=null,Memo36=null ")
                sql.AppendFormat("Where 1=1 ")
                sql.AppendFormat("And SapCode in ")
                sql.AppendFormat("( ")
                sql.AppendFormat("  Select SapCode from Productdata where Left(Prodhierarchy,2)='{0}' ", Me.ddlSignature.SelectedValue)
                sql.AppendFormat(") ")
                sql.AppendFormat("And Version1 ='S' ")
                sql.AppendFormat("And Skutype not in('ListP','PAllow','STD') ")
                sql.AppendFormat(";Delete from {0}SFReportMemo{1} where 1=1 ", Me.HeaderTB, Me.VersionControl1.Year)
                sql.AppendFormat("And MonthCode='{0}' ", Me.VersionControl1.MonthCode)
                sql.AppendFormat("And SapCode in ")
                sql.AppendFormat("( ")
                sql.AppendFormat("  Select SapCode from Productdata where Left(Prodhierarchy,2)='{0}' ", Me.ddlSignature.SelectedValue)
                sql.AppendFormat(") ")
                sql.AppendFormat("And Skutype not in('ListP','PAllow','STD','SellOut','SellOut(Y-1)','SellOutPBOM(Y-1)') ")
            Case "TCReport"
                'sql.AppendFormat(",Mon1Notes=null,Mon2Notes=null,Mon3Notes=null,Mon4Notes=null,Mon5Notes=null,Mon6Notes=null ")
                'sql.AppendFormat(",Mon7Notes=null,Mon8Notes=null,Mon9Notes=null,Mon10Notes=null,Mon11Notes=null,Mon12Notes=null ")
                'sql.AppendFormat(",Mon13Notes=null,Mon14Notes=null,Mon15Notes=null,Mon16Notes=null,Mon17Notes=null,Mon18Notes=null ")
                'sql.AppendFormat(",Mon19Notes=null,Mon20Notes=null,Mon21Notes=null,Mon22Notes=null,Mon23Notes=null,Mon24Notes=null ")
                sql.AppendFormat("Where 1=1 ")
                sql.AppendFormat("And Left(Hierarchy,2)='{0}' ", Me.ddlSignature.SelectedValue)

                sql.AppendFormat(";Delete from {0}TCReportFixedPercentage where 1=1 ", Me.HeaderTB)
                sql.AppendFormat("And YYMM='{0}' ", Me.VersionControl1.MonthCode)
                sql.AppendFormat("And Signature='{0}' ", Me.ddlSignature.SelectedValue)

                sql.AppendFormat(";Delete from {0}TCReportMemo{1} where 1=1 ", Me.HeaderTB, Me.VersionControl1.Year)
                sql.AppendFormat("And MonthCode='{0}' ", Me.VersionControl1.MonthCode)
                sql.AppendFormat("And Left(Hierarchy,2)='{0}' ", Me.ddlSignature.SelectedValue)
        End Select

        Try
            'MyObj.WriteLog(sql.ToString())
            dat.AccData(sql)

            If Me.ddlCategory.SelectedValue = "SFReport" AndAlso Me.ddlUploadType.SelectedValue = "SellIn" AndAlso Right(TableName, 1) <> "B" AndAlso Right(TableName, 1) <> "T" Then
                sql.Length = 0
                sql.AppendFormat("Delete from FM_SFReport{0} where 1=1 ", Me.VersionControl1.MonthCode)
                sql.AppendFormat("And SapCode in ")
                sql.AppendFormat("( ")
                sql.AppendFormat("  Select SapCode from Productdata where Left(Prodhierarchy,2)='{0}' ", Me.ddlSignature.SelectedValue)
                sql.AppendFormat(") ")
                'MyObj.WriteLog(sql.ToString())
                dat.AccData(sql)
            End If

            MyObj.ShowMessage("Clear Successful!")
        Catch ex As Exception
            ErrorMessage = ex.ToString()
            Success = False
            MyObj.ShowMessage("Clear Failed!")
        End Try

        sql.Length = 0
        sql.AppendFormat("Insert into ClearVersionRecord(Version,Signature,Category,IPAddress,UpdateResult,UpdateBy,UpdateDate) ")
        sql.AppendFormat("select ")
        sql.AppendFormat(" '{0}' ", Me.VersionControl1.MonthCode)
        sql.AppendFormat(",'{0}' ", Me.ddlSignature.SelectedValue)
        sql.AppendFormat(",'{0}' ", Me.ddlCategory.SelectedItem.Text & "-" & Me.ddlUploadType.SelectedItem.Text)
        sql.AppendFormat(",'{0}' ", MyObj.GetClientIP())
        sql.AppendFormat(",'{0}' ", IIf(Success, "Successful", ErrorMessage))
        sql.AppendFormat(",'{0}' ", clsAuth.GetUserID())
        sql.AppendFormat(",{0} ", "getdate()")
        dat.AccData(sql)

        Me.BindGV()
    End Sub
    Protected Sub ddlDivision_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDivision.SelectedIndexChanged
        Me.BindDDLSignature()
    End Sub
    Protected Sub ddlCategory_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlCategory.SelectedIndexChanged
        Me.BindDDLUploadType()
    End Sub
    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then

        End If
    End Sub
#End Region

#Region "繫結資料"
    Private Sub BindDDLDivision()
        Dim ddlControl As DropDownList = Me.ddlDivision
        Dim dvManager As New DivisionManager

        ddlControl.DataTextField = "Division"
        ddlControl.DataValueField = "Division"
        ddlControl.DataSource = dvManager.GetAllDivision()
        ddlControl.DataBind()
    End Sub
    Private Sub BindDDLSignature()
        Dim ddlControl As DropDownList = Me.ddlSignature
        Dim stManager As New SignatureManager

        ddlControl.DataTextField = "Description"
        ddlControl.DataValueField = "Signature"
        ddlControl.DataSource = stManager.GetAllSignature(Me.ddlDivision.SelectedValue)
        ddlControl.DataBind()
    End Sub
    Private Sub BindDDLCategory()
        Dim ddlControl As DropDownList = Me.ddlCategory
        ddlControl.Items.Clear()
        ddlControl.Items.Add(New ListItem("Sales Forecast", "SFReport"))
        ddlControl.Items.Add(New ListItem("Finance Figures", "TCReport"))
    End Sub
    Protected Sub BindDDLUploadType()
        Dim ddlControl As DropDownList = Me.ddlUploadType
        ddlControl.Items.Clear()
        If Me.ddlCategory.SelectedValue = "SFReport" Then
            ddlControl.Items.Add(New ListItem("Sell In", "SellIn"))
            ddlControl.Items.Add(New ListItem("Sell Out", "SellOut"))
            ddlControl.Items.Add(New ListItem("PLV", "PLV"))
            ddlControl.Items.Add(New ListItem("Working File", "WF"))
        ElseIf Me.ddlCategory.SelectedValue = "TCReport" Then
            ddlControl.Items.Add(New ListItem("Sell In", "SellIn"))
            ddlControl.Items.Add(New ListItem("Sell Out", "SellOut"))
        End If
    End Sub
    Public Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0
        sql.AppendFormat("select crv.UpdateBy ")
        sql.AppendFormat(",convert(varchar(10),crv.UpdateDate,111) as UpdateDate ")
        sql.AppendFormat(",convert(varchar(10),crv.UpdateDate,108) as UpdateTime ")
        sql.AppendFormat(",crv.Version,crv.Category,crv.IPAddress,crv.UpdateResult ")
        sql.AppendFormat(",dbm.Division,po.Description as SignatureText ")
        sql.AppendFormat("from ClearVersionRecord crv ")
        sql.AppendFormat("left join DivisionBrandMappingView dbm on dbm.Brand = crv.Signature ")
        sql.AppendFormat("left join ProducthierarchyOriginal po on po.Code = crv.Signature ")
        sql.AppendFormat("order by crv.UpdateDate desc ")

        gv.DataSource = dat.GetData(sql)
        gv.DataBind()

    End Sub
#End Region

    Public ReadOnly Property HeaderTB() As String
        Get
            Select Case Me.ddlUploadType.SelectedValue
                Case "SellOut"
                    Return "SO_"
                Case "PLV"
                    Return "PLV_"
                Case "WF"
                    Return "WF_"
                Case Else
                    Return ""
            End Select
        End Get
    End Property

End Class
